package com.xhwl.centiotdata.suppliersystem.itc;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.ItcConstant;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.common.pojo.cent.sdata.Organization;
import com.xhwl.common.pojo.cent.sdata.personnel.StaffDTO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author： wangyiling
 * @Description：itc会议系统 添加人员/组织
 * @Date 2022/9/16
 */
@Service
public class UserItcHandler {
    private static final Logger log = LoggerFactory.getLogger(UserItcHandler.class);

    @Autowired
    private GeneralItcHandler generalItcHandler;

    public String addPerson(StaffDTO staffDTO, PedestrianIntegrationConfigDTO configDTO) {
        JSONObject resultObject = new JSONObject();
        String[] strs = staffDTO.getLoginName().split("-");
        String imageUrl = generalItcHandler.upload(ItcConstant.UPLOAD_IMAGE_URL, staffDTO.getFaceUrl(), configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());

        if (null != staffDTO.getId()) {
            JSONObject fields = new JSONObject();
            fields.put("displayname", staffDTO.getName());
            fields.put("sex", "男");
            fields.put("mobile", staffDTO.getPhone());
            fields.put("jobTitle", strs[1]);
            JSONObject param = new JSONObject();
            param.put("fields", fields);
            if (StringUtils.isNotEmpty(imageUrl)) {
                param.put("imagePath", imageUrl);
            }
            resultObject = generalItcHandler.put(ItcConstant.ADD_PERSON_URL + "/" + strs[0], param, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
            //更新用户部门
            JSONObject update = new JSONObject();
            update.put("newIds", staffDTO.getOrgIds());
            update.put("uid", strs[0]);
            generalItcHandler.put(ItcConstant.UPDATE_PERSON_ORG_URL, update, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        } else {
            JSONObject param = new JSONObject();
            param.put("userid", strs[0]);
            param.put("password", staffDTO.getPassword());
            param.put("displayname", staffDTO.getName());
            param.put("sex", "男");
            param.put("mobile", staffDTO.getPhone());
            if (StringUtils.isNotEmpty(imageUrl)) {
                param.put("imagePath", imageUrl);
            }
            List<JSONObject> departList = new ArrayList<>();
            staffDTO.getOrgIds().forEach(id -> {
                JSONObject depart = new JSONObject();
                depart.put("id", id);
                depart.put("job_title", strs[1]);
                departList.add(depart);
            });
            param.put("departmentIds", departList);
            resultObject = generalItcHandler.post(ItcConstant.ADD_PERSON_URL, param, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        }
        JSONObject jsonObject = resultObject.getJSONObject("data");
        String userId = "";
        if (jsonObject != null) {
            userId = jsonObject.getString("id");
        }
        return userId;
    }

    public void deleteUser(String id, PedestrianIntegrationConfigDTO configDTO) {
        JSONObject param = new JSONObject();
        param.put("id", id);
        JSONObject resultObject = generalItcHandler.delete(ItcConstant.ADD_PERSON_URL, param, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        resultObject.getJSONObject("data");
    }

    public String addOrg(Organization organization, PedestrianIntegrationConfigDTO configDTO) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("name", organization.getName());
        JSONObject resultObject = new JSONObject();
        if (null != organization.getId()) {
            resultObject = generalItcHandler.putOrg(ItcConstant.ADD_ORG_URL + "/" + organization.getId(), "name=" + organization.getName(), configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        } else {
            param.put("parentId", organization.getParentId());
            resultObject = generalItcHandler.post(ItcConstant.ADD_ORG_URL, param, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        }
        JSONObject jsonObject = resultObject.getJSONObject("data");
        String orgId = "";
        if (jsonObject != null) {
            orgId = jsonObject.getString("id");
        }
        return orgId;
    }

    public void deleteOrg(Organization organization, PedestrianIntegrationConfigDTO configDTO) {
        JSONObject param = new JSONObject();
        param.put("id", organization.getId());
        generalItcHandler.delete(ItcConstant.ADD_ORG_URL, param, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
    }

    public String addLocation(Organization organization, PedestrianIntegrationConfigDTO configDTO) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("name", organization.getName());
        JSONObject resultObject;
        if (null != organization.getId()) {
            resultObject = generalItcHandler.putOrg(ItcConstant.ADD_LOCATION_URL + "/" + organization.getId(), "name=" + organization.getName(), configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        } else {
            param.put("parentId", organization.getParentId());
            resultObject = generalItcHandler.post(ItcConstant.ADD_LOCATION_URL, param, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        }
        JSONObject jsonObject = resultObject.getJSONObject("data");
        String orgId = "";
        if (jsonObject != null) {
            orgId = jsonObject.getString("id");
        }
        return orgId;
    }

    public String deleteLocation(Organization organization, PedestrianIntegrationConfigDTO configDTO) {
        JSONObject param = new JSONObject();
        param.put("id", organization.getId());
        JSONObject resultObject = generalItcHandler.delete(ItcConstant.ADD_LOCATION_URL, param, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        JSONObject jsonObject = resultObject.getJSONObject("data");
        return jsonObject.getString("code");
    }
}
